package engine;

import serv.ThreadManager;

public class CheckWorld {
	
	private int taille_chaine;
	
	public CheckWorld(int nbp, int nbf){
		this.taille_chaine = (5 + (nbp-1)*14 + nbf*10);
	}
	
	// Scan the world and return only what the client has to draw
	public String check (World w, int nbt, ThreadManager tm){
		
		int x;
		int y;
		int a = w.getPlayers()[nbt].getAngle();
		String sx = "";
		String sy = "";
		String sa = "";
		if (a<10) {sa = "00"+a;}
		else if (a<100) {sa = "0"+a;}
		else {sa = ""+a;}
		String rsp = "p:"+sa;
		
		//Scan all the players and check if they are visible in the  main screen
		for (int i=0; i<w.getNbPlayersNow(); i++){
			if (i != nbt){
				if ((tm.isFree(i)==false)
					&&(w.getPlayers()[i].getPlayed())
					&&((w.getPlayers()[i].getPosX() >= (w.getPlayers()[nbt].getPosX() - 260))
					|| (w.getPlayers()[i].getPosX() <= (w.getPlayers()[nbt].getPosX() + 260)))
					&&((w.getPlayers()[i].getPosY() >= (w.getPlayers()[nbt].getPosY() - 156)) 
					|| (w.getPlayers()[i].getPosY() <= (w.getPlayers()[nbt].getPosY() + 156)))){
					
					x = w.getPlayers()[i].getPosX() - w.getPlayers()[nbt].getPosX() + 240;
					y = w.getPlayers()[i].getPosY() - w.getPlayers()[nbt].getPosY() + 136;
					a = w.getPlayers()[i].getAngle();
					sx = "";
					sy = "";
					sa = "";
					if (x<10) {sx = "00"+x;}
					else if (x<100) {sx = "0"+x;}
					else {sx = ""+x;}
					if (y<10) {sy = "00"+y;}
					else if (y<100) {sy = "0"+y;}
					else {sy = ""+y;}
					if (a<10) {sa = "00"+a;}
					else if (a<100) {sa = "0"+a;}
					else {sa = ""+a;}
					rsp = rsp+"!e:"+sx+":"+sy+":"+sa;
				}
			}
		}
		
		for (int i=0; i<w.getNbFanion(); i++){
			if (  ((w.getFanion()[i].getPosX() >= (w.getPlayers()[nbt].getPosX() - 240))
				|| (w.getFanion()[i].getPosX() <= (w.getPlayers()[nbt].getPosX() + 240)))
				&&((w.getFanion()[i].getPosY() >= (w.getPlayers()[nbt].getPosY() - 136)) 
				|| (w.getFanion()[i].getPosY() <= (w.getPlayers()[nbt].getPosY() + 136)))){
				
				x = w.getFanion()[i].getPosX();
				y = w.getFanion()[i].getPosY();
				sx = "";
				sy = "";
				if (x<10) {sx = "00"+x;}
				else if (x<100) {sx = "0"+x;}
				else {sx = ""+x;}
				if (y<10) {sy = "00"+y;}
				else if (y<100) {sy = "0"+y;}
				else {sy = ""+y;}
				rsp = rsp+"!f:"+sx+":"+sy;
			}
		}
		rsp = rsp+"!*****";
		if (rsp.length() < (taille_chaine)){
			int diff = taille_chaine - rsp.length();
			for (int i=0; i<diff; i++){
				rsp = rsp+"*";
			}
		}
		
		return rsp;
	}
}
